package JasPerl::Compiler::NameMangler;

use strict;
use warnings;

sub new {
    my $class = shift;
    bless { }, $class;
}

sub mangle {
    my ($self, $name) = @_;
    $name =~ s/(\W|_)+/_/g;
    $name =~ s/([[:lower:]])([[:upper:]]+)/$1_\L$2\E/g;
    return $name;
}

sub mangle_expr {
    my ($self, $name) = @_;
    my $var = $self->mangle("$name");
    return sprintf("%s_%04x", $var, $self->{$var}++);
}

sub mangle_tag {
    my ($self, $name) = @_;
    my $tagvar = $self->mangle("jspx_tag_$name");
    return sprintf("%s_%04x", $tagvar, $self->{$tagvar}++);
}

sub mangle_method {
    my ($self, $name) = @_;
    my $method = $self->mangle("_jspx_meth_$name");
    return sprintf("%s_%04x", $method, $self->{$method}++);
}

1;

__END__
